page.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { notFound } from 'next/navigation';
  2. import { allPosts } from 'contentlayer/generated';
  3. import Mdx from '@/components/MDX';
  4. interface PageProps {
  5. params: {
  6. slug: string
  7. }
  8. }
  9. async function getPageFromParams(params) {
  10. const slug = params?.slug,
  11. page = allPosts.find((page) => page.slugAsParams === slug);
  12. if (!page) {
  13. null;
  14. }
  15. return page;
  16. }
  17. export async function generateStaticParams(): Promise<PageProps['params'][]> {
  18. return allPosts.map((page) => ({
  19. slug: page.slugAsParams,
  20. }));
  21. }
  22. export default async function PostPage({ params }: PageProps) {
  23. const post = await getPageFromParams(params);
  24. if (!post) {
  25. notFound();
  26. }
  27. return (
  28. <>
  29. {post && post.title && (
  30. <div className="hero">
  31. <div className="container container-narrow">
  32. <div className="hero-subheader">{post.product}</div>
  33. <h1 className="hero-title">{post.title}</h1>
  34. </div>
  35. </div>
  36. )}
  37. <section className="section pt-0">
  38. <div className="container container-narrow">
  39. <div className="markdown">
  40. {post.description && <p className="lead">{post.description}</p>}
  41. <Mdx code={post.body.code} />
  42. </div>
  43. </div>
  44. </section>
  45. </>
  46. );
  47. }